package com.lbs.jiepiaisi.biz;

import android.content.Intent;

import com.lbs.jiepiaisi.bean.BaseResponseEntity;
import com.lbs.jiepiaisi.manager.App;
import com.lbs.jiepiaisi.manager.SharePreferenceManager;
import com.lbs.jiepiaisi.parser.BaseParser;
import com.lbs.jiepiaisi.utils.Constant;
import com.lbs.jiepiaisi.utils.HttpUtil;
import com.lbs.jiepiaisi.utils.UrlUtil;

import org.json.JSONObject;

import okhttp3.FormBody;
import okhttp3.Response;

/**
 *
 * 时间：2018/1/11 15:40
 * 作用：修改密码
 */

public class UpdatePwdBiz {
    public void updatePwdBiz(String password) {
        SharePreferenceManager mSharePreference=SharePreferenceManager.getInstance(App.instance.getApplicationContext());
        String userId=mSharePreference.getInt(Constant.ACCOUNT_USERID,Constant.SP_NULL) +"";
        FormBody formBody = new FormBody
                .Builder()
                .add("userId",userId)//设置参数名称和参数值
                .add("password",password)//设置参数名称和参数值
                .build();
        HttpUtil.getHttpUtil().post(UrlUtil.ACCOUNT_UPDATE_PWD_URL,formBody, new HttpUtil.Listener() {
            @Override
            public void success(Response response) {
                try{
                    String jsonString = response.body().string();//返回结果
                    JSONObject jsonObject = new JSONObject(jsonString);//转换结果
                    BaseResponseEntity baseResponseEntity= BaseParser.parser(jsonObject.toString());//json解析
                    Intent intent = new Intent(Constant.ACCOUNT_UPDATE_PWD);
                    baseResponseEntity.setStatus(Constant.STATUS_OK);
                    intent.putExtra(Constant.KEY_DATA, baseResponseEntity);
                    App.instance.sendBroadcast(intent);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            @Override
            public void failed() {
                BaseResponseEntity baseResponseEntity=new BaseResponseEntity();
                baseResponseEntity.setStatus(Constant.STATUS_NETWORK_ERROR);
                Intent intent = new Intent(Constant.ACCOUNT_UPDATE_PWD);
                intent.putExtra(Constant.KEY_DATA, baseResponseEntity);
                App.instance.sendBroadcast(intent);
            }
            @Override
            public void noNetwork() {
                BaseResponseEntity baseResponseEntity=new BaseResponseEntity();
                baseResponseEntity.setStatus(Constant.STATUS_NO_NET);
                Intent intent = new Intent(Constant.ACCOUNT_UPDATE_PWD);
                intent.putExtra(Constant.KEY_DATA, baseResponseEntity);
                App.instance.sendBroadcast(intent);
            }
        });
    }
}
